package com.sola.oauthserver.config2;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
/**
 * JWT配置类
 * */
@Configuration
public class JWTConfig {

    @Value("${security.oauth2.resource.jwt.key-value}")
    private String SigningKey;
   //创建一个JWTtokenstore
    @Bean
    public TokenStore jwtTokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /**
     * 令牌转换器：指定签名
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
        //sola-town为对称加密的密钥，所有需要解析JWT的微服务都需要持有这个密钥
        accessTokenConverter.setSigningKey(SigningKey);
        return accessTokenConverter;
    }

    //声明一个令牌增强器，具体实现，在JwtTokenEnhancer类中
    @Bean
    public TokenEnhancer jwtTokenEnhancer(){
        return new JwtTokenEnhancer();
    }
}
